package com.j4it.migration.modules.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.j4it.migration.common.persistence.Page;
import com.j4it.migration.common.utils.StringUtils;
import com.j4it.migration.common.web.BaseController;
import com.j4it.migration.modules.cms.entity.TableState;
import com.j4it.migration.modules.cms.service.TableStateService;

@Controller
public class FrontController extends BaseController {

    @Autowired
    private TableStateService tableStateService;

    @ModelAttribute
    public TableState get() {
        return new TableState();
    }

    @RequestMapping(value = {"/{path}"})
    public String index(@PathVariable String path, TableState tableState, HttpServletRequest request, HttpServletResponse response, Model model) {
        // 打开首页时，默认搜索"saofu"
        if (path.equals("index")) {
            tableState.setTableSchema("saofu");
        }
        // date格式转换
        // String resource = request.getParameter("createTime");
        // if (StringUtils.isNotBlank(resource)) {
        // int createTime = StringUtils.dateToInt(resource);
        // tableState.setCreateTime(createTime);
        // }
        Page<TableState> page = new Page<TableState>(request, response);

        if (StringUtils.isNotBlank(tableState.getTableSchema())) {
            page = tableStateService.findPage(page, tableState);
        }
        model.addAttribute("page", page);
        return "modules/cms/frontIndex";
    }

    @RequestMapping(value = "/show/increment")
    public String increment(TableState tableState, HttpServletRequest request, Model model) {
        tableState.setTableSchema(request.getParameter("tableSchema"));
        tableState.setTableName(request.getParameter("tableName"));
        tableState.setCreateTime(Integer.parseInt(request.getParameter("createTime")));
        List<List<TableState>> list = tableStateService.findListInterval(tableState);
        model.addAttribute("resource", list.get(0));
        model.addAttribute("increment", list.get(1));
        return "modules/cms/increment";
    }

}
